package com.ymk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ymk.constant.MessageConstant;
import com.ymk.pojo.TravelGroup;
import com.ymk.service.TravelGroupService;
import com.ymk.vo.PageResult;
import com.ymk.vo.QueryPageBean;
import com.ymk.vo.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.applet.resources.MsgAppletViewer;

import java.util.List;

@RequestMapping("/travelGroup")
@RestController
public class TravelGroupController {

    @Reference
    private TravelGroupService travelGroupService;

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('TRAVELGROUP_ADD')")//权限校验
    public Result add(@RequestBody TravelGroup travelGroup,
                      Integer[] travelItemIds){
        try {
            travelGroupService.add(travelGroup,travelItemIds);
            return new Result(true, MessageConstant.ADD_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_TRAVELGROUP_FAIL);
        }
    }

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('TRAVELGROUP_QUERY')")//权限校验
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = travelGroupService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    //使用自由行id，查询跟团游和自由行的中间表获取自由行的集合，存放id的值
    @RequestMapping("/findTravelItemByTravelGroupId")
    public List<Integer> findTravelItemByTravelGroupId(Integer id){
        List<Integer> list =
                travelGroupService.findTravelItemByTravelGroupId(id);
        return list;
    }

    //使用id查询跟团游，进行表单回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            TravelGroup travelGroup = travelGroupService.findById(id);
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,travelGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    //编辑跟团游(返回public Result(boolean flag,String message))
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('TRAVELGROUP_EDIT')")//权限校验
    public Result edit(Integer[] travelItemIds,@RequestBody TravelGroup travelGroup){
        try {
            travelGroupService.edit(travelItemIds,travelGroup);
            return new Result(true,MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_TRAVELGROUP_FAIL);
        }
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('TRAVELGROUP_DELETE')")//权限校验
    public Result delete(Integer id){
        try {
            travelGroupService.delete(id);
            return new Result(true,MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }
    }

    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('TRAVELGROUP_QUERY')")//权限校验
    public Result findAll(){
        try {
            List<TravelGroup> lists =travelGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_TRAVELGROUP_SUCCESS,lists);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

}
